package com.jr.controller;


import com.jr.entry.User;
import com.jr.service.UserService;


import com.jr.util.ResultObject;
import com.jr.util.ResultObject;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/User/")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ResultObject resultObject;

    @RequestMapping("selectphone")
    public ResultObject selectphone(User user){
        boolean b = userService.selectByUsername(user);
        if (b){
            resultObject.setStatus(400);
            resultObject.setResult("手机号已存在");
        }else{
            resultObject.setStatus(404);
            resultObject.setResult("手机号可用");
        }
        return resultObject;
    }
    @RequestMapping("add")
    public ResultObject add(User user){
        user.setUsername(user.getPhone());
        boolean b = userService.addUser(user);
        if (b){
            resultObject.setStatus(400);
            resultObject.setResult("成功");
        }else{
            resultObject.setStatus(404);
            resultObject.setResult("失败");
        }
        return resultObject;
    }




    /*
     * 用户名登录功能，100为成功，101为失败
     * */
    @RequestMapping("login")
    public ResultObject userLogin(User user) {
        boolean b = userService.selectUser(user);
        ArrayList<User> list = userService.userLogin(user);
        if (b) {
            resultObject.setStatus(100);
            resultObject.setResult(list.get(0).getUid());
        } else {
            resultObject.setStatus(101);
            resultObject.setResult(0);
        }

        return resultObject;
    }
    /*
     * 登录功能三选一，100为成功，101为失败
     * */
    @RequestMapping("selectlogin")
    public ResultObject loginByOne(User user){
        ArrayList<User> user1 = userService.selectUserByOne(user);
        System.out.println(user1);
        if(!user1.isEmpty()){
            resultObject.setStatus(100);
            resultObject.setResult(user1.get(0).getUid());
        }else{
            resultObject.setStatus(101);
//            resultObject.setResult(user1.get(0).getUid());
        }
        System.out.println("resultObject"+resultObject);
        return resultObject;
    }

    @RequestMapping("forgetlogin")
    public User forgetlogin(User user){
        System.out.println("user"+user);
        User forgetlogin = userService.forgetlogin(user);
        return forgetlogin;
    }

    @RequestMapping("updatepassword")
    public Boolean updatepassword(User user){
        System.out.println("Password:"+user.getPassword());
        System.out.println("username:"+user.getUsername());
        Boolean updatepassword = userService.updatepassword(user);
        return updatepassword;
    }

}
